/**
 * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package doan.quanlycanbo.portlet.service.impl;

import java.util.List;

import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.ResourceConstants;

import doan.quanlycanbo.portlet.NoSuchhochamException;
import doan.quanlycanbo.portlet.model.hocham;
import doan.quanlycanbo.portlet.service.base.hochamLocalServiceBaseImpl;

/**
 *
 * @author Nguyen Tuan Vinh
 */
public class hochamLocalServiceImpl extends hochamLocalServiceBaseImpl {
	
	public List<hocham> getAll() throws SystemException{
		return hochamPersistence.findAll();
	}
	
	public hocham addHocHam(hocham hocham1, long userId) throws SystemException{
		long mahocham = CounterLocalServiceUtil.increment(hocham.class.getName());
		hocham hocham2 = hochamPersistence.create(mahocham);
		hocham2.setGroupId(hocham1.getGroupId());
		hocham2.setCompanyId(hocham1.getCompanyId());
		hocham2.setLanguageId(hocham1.getLanguageId());
		hocham2.setTenhocham(hocham1.getTenhocham());

		try {
			resourceLocalService.addResources(hocham1.getCompanyId(), hocham1.getGroupId(), userId, 
					hocham.class.getName(), hocham2.getPrimaryKey(), false, true, true);
		} catch (PortalException e) {
			e.printStackTrace();
		}
		
		return hochamPersistence.update(hocham2, false);
	}
	
	public hocham updateHocHam(long mahocham, String tenhocham) throws SystemException, NoSuchhochamException{
		hocham hocham = hochamPersistence.findByPrimaryKey(mahocham);
		hocham.setTenhocham(tenhocham);
		return hochamPersistence.update(hocham, false);
	}
	
	public hocham deleteHocHam(long mahocham) throws SystemException, NoSuchhochamException,PortalException{
		hocham hocHam = hochamPersistence.findByPrimaryKey(mahocham);
		return deleteHocHam(hocHam);
	}
	
	public hocham deleteHocHam(hocham hocHam) throws SystemException,PortalException{
		resourceLocalService.deleteResource(hocHam.getCompanyId(), hocham.class.getName(), ResourceConstants.SCOPE_INDIVIDUAL, hocHam.getPrimaryKey());
		return hochamPersistence.remove(hocHam);
	}
}